<?php
namespace app\common\traits;
/**
 * Created by PhpStorm.
 * User: HuangLuSen
 * Date: 2017/5/4
 * Time: 14:04
 */
use think\Facade\Config;
use page\Page as Pages;
use app\common\model\Leibie;
trait Page{
    /**
     * 分页
     * @param $tablename      表名
     * @param string $data    条件
     * @param int $pagesize   每页分页条数
     */
    public function pageshow($tablename, $data='',$nullzhi='',$with='',$order='id desc', $pagesize=20)
    {
        $model = model($tablename);
        if(empty($nullzhi)){
            if(isset($data['where'])){
                $where = $data['where'];
                unset($data['where']);
                $count  = $model->where($data)->where(function ($query) use ($where){
                    $query->where('jdry','=',$where['jdry'])
                    ->whereOr('fkry','=',$where['jdry'])
                    ->whereOr(function ($que) use ($where){
                            $que->where('yiyuan','in',$where['yiyuan'])->where('yctime','between',[time(),$where['yctime'] ]);
                        });
                })->with($with)->fetchSql(false)->count();// 查询满足要求的总记录数
            }else{
                $count  = $model->where($data)->with($with)->count();// 查询满足要求的总记录数
            }
        }else{
            $count  = $model->where($nullzhi,null)->where($data)->with($with)->count();// 查询满足要求的总记录数
        }
        $Page       = new Pages($count,$pagesize);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');//定义分页类的的主题
        $show       = $Page->newshow();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        if(empty($nullzhi)){
                if(isset($where)){
                    $list = $model->where($data)->where(function ($query) use ($where){
                        $query->where('jdry','=',$where['jdry'])
                            ->whereOr('fkry','=',$where['jdry'])
                            ->whereOr(function ($que) use ($where){
                                $que->where('yiyuan','in',$where['yiyuan'])->where('yctime','between',[time(),$where['yctime'] ]);
                            });
                    })->with($with)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();
                }else{
                    $list = $model->where($data)->with($with)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();
                }
            }else{
                $list = $model->where($nullzhi,null)->where($data)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();//会所门店为空

        }
//            echo $model->getLastSql();
        $this->assign('count',$count);
        $this->assign('page',$show);
        return $list;
    }
    /**
     * 订单专用
     * @param $tablename      表名
     * @param string $data    条件
     * @param int $pagesize   每页分页条数
     */
    public function pageorder($tablename, $data='',$nullzhi='',$with='',$order='id desc', $pagesize=20)
    {
        $model = model($tablename);
        if(empty($nullzhi)){
            $count  = $model->where($data)->with($with)->count();// 查询满足要求的总记录数
        }else{
            $count  = $model->where($nullzhi,null)->where($data)->with($with)->count();// 查询满足要求的总记录数
        }
        $Page       = new Pages($count,$pagesize);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');//定义分页类的的主题
        $show       = $Page->newshow();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性

        if(empty($nullzhi)){
            $list = $model->where($data)->with($with)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();
        }else{
            $list = $model->where($nullzhi,null)->where($data)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();//会所门店为空
        }
//        dump($list);
//        echo $model->getLastSql();
        $this->assign('count',$count);
        $this->assign('page',$show);
        return $list;
    }
    /**  处理电话、微信 信息加密
     * @param $data
     * @return mixed
     */
    public function protectData($data,$keys = ['maphone','otherphone','weixin','qq']){
        if (empty($data))
            return '';
        if(is_string($keys)){
            $keys = explode(',',$keys);
        }
        $data = $data->append(['state_text','qdxx_text','yyxx_text','kcxx_text','lbxx_text','bumen_text','area_text','hszt_text','yykf_text','jdry_text','hsmd_text','yycg_text','khd_text','operation_text','czry_text','fkry_text','yyreason_text','color_text','zjsry_text'])->toArray();
        foreach ($data as $key=>$value){
            if (is_array($value)){
                foreach ($value as $k=>$v){
                    if( $this->is_Baomi($value) && in_array($k,$keys) ) {
                        $data[$key][$k] = hidInfo($v);
                    }
                }
            }else{//一维数组
                if( $this->is_Baomi($data) && in_array($key,$keys) ){
                        $data[$key] = hidInfo($value);
                }
            }
        }
        return $data;
    }

    public function is_Baomi($data){
        if($this->hasRole('qudaozhuanyuan')){
            $yiyuan = $this->getUserToYiyuan();
            //渠道专员负责的医院不加密
            if( !empty($yiyuan) && in_array($data['yiyuan'],explode(',',$yiyuan)) ){
                return false;
            }
        }
        if($this->getBaomiqx() == 1){
            $hidStates = $this->getHidStates();//需要加密的状态
            if( !empty($hidStates) ){
                if( in_array($data['ywstate'],$hidStates ) ){
                    return true;
                }
                //邀约客服不是自己的订单需要保密
                if( $this->hasRole('yaoyuezhuguan') && $data['yykf'] != session('admin_user.adminid') ){
                    return true;
                }
            }
        }
        return false;
    }


//    public function protectData($data){
//        if($this->getBaomiqx() == 1){
//            $condition = $this->getHidStates();//需要加密的状态
//            if($this->hasRole('yaoyuezhuguan')){
//                $data = doData($data,$condition,session('admin_user.adminid'),'yykf');
//            }else{
//                $data = doData($data,$condition);
//            }
//        }
//
//        return $data;
//    }

    /**
     *  获取相关数据
     */
    public function getSj( $hsdzid='' ){
        $leibie = new Leibie();
        $city = $leibie->getCity();
        $area = $leibie->getArea();
        $qudao = $leibie->getQudao();
        $fenlei  = $leibie->getFenlei();
        $yiyuan  =$leibie->getYiyuan();
        $kecheng = $leibie->getKecheng();
        $hsstate   = $leibie->getHsstate();
        $qdkf = $leibie->getQdkf();
        $wlkf = $leibie->getWlkf();
        $jdkf = $leibie->getJdkf();
        $yykf = $leibie->getYykf();
        $yycg = $leibie->getYycg();
        $hdmd = $leibie->getHdmd();/* 活动门店 */
        $ywstate = $leibie->getYwstate();
        $hszy = $leibie->getHszy( $hsdzid );
        $bumen = $leibie->getBumen();
        $menzhen=$leibie->getMenzhen();
        $fkry = $leibie->getFkry();
        $daoru = $leibie->getDdlx();//订单类型
        $yaoyueReason = $leibie->getYaoyueReason();//邀约原因

        $editstates = $this->getEditStates();
        foreach ($ywstate as $value){
            if(in_array($value['id'],$editstates)){
                $state[] = ['id'=>$value['id'],'name'=>$value['name']];
            }else{
                $state[] = ['id'=>$value['id'],'name'=>$value['name'],'status'=>'disabled="disabled" style="color:#c3cbd6"'];
            }
        }

        $this->assign('state',$state);
        $this->assign('city',$city);
        $this->assign('area',$area);
        $this->assign('menzhen',$menzhen);
        $this->assign('qudao',$qudao);
        $this->assign('fenlei',$fenlei);
        $this->assign('yiyuan',$yiyuan);
        $this->assign('menzhen',$menzhen);
        $this->assign('kecheng',$kecheng);
        $this->assign('hsstate',$hsstate);
        $this->assign('qdkf',$qdkf);
        $this->assign('wlkf',$wlkf);
        $this->assign('jdkf',$jdkf);
        $this->assign('yykf',$yykf);
        $this->assign('yycg',$yycg);
        $this->assign('hdmd',$hdmd);
        $this->assign('ywstate',$ywstate);
        $this->assign('hszy',$hszy);
        $this->assign('bumen',$bumen);
        $this->assign('fkry',$fkry);
        $this->assign('daoru',$daoru);
        $this->assign('yaoyueReason',$yaoyueReason);
    }
    //获取分配相关数据
    public function getFpSj( $areadata = '',$hsdzid='' ){
        $leibie = new Leibie();
        $area = $leibie->getArea();
        $qudao = $leibie->getQudao();
        $yiyuan  =$leibie->getYiyuan($areadata);
        $ywstate = $leibie->getYwstate();
        $yykf = $leibie->getYykf();
        $hdmd = $leibie->getHdmd();/* 活动门店 */

        $this->assign('area',$area);
        $this->assign('qudao',$qudao);
        $this->assign('yiyuan',$yiyuan);
        $this->assign('ywstate',$ywstate);
        $this->assign('yykf',$yykf);
        $this->assign('hdmd',$hdmd);
    }


    public function getOlistMoban(){
        if($this->hasRole('qudaojingli') || $this->hasRole('qudaozhuguan') || $this->hasRole('qudaozhuanyuan')){
            return $this->fetch('olist-qudao');
        }elseif($this->hasRole('wangluojingli') || $this->hasRole('wangluozhuguan') || $this->hasRole('wangluozhuanyuan')){
            return $this->fetch('olist-wangluo');
        }elseif($this->hasRole('yaoyuejingli') || $this->hasRole('yaoyuezhuguan') || $this->hasRole('yaoyuezhuanyuan')){
            return $this->fetch('olist-yaoyue');
        }elseif( $this->hasRole(['sheyingjingli','sheyingdiaodu']) ){
            return $this->fetch('olist-sheying');
        }elseif($this->hasRole('huodongjingli') || $this->hasRole('huodongzhuguan') || $this->hasRole('huodongzhuanyuan')){
            return $this->fetch('olist-huisuo');
        }else{
            return $this->fetch('olist');
        }

    }

    public function getAddMoban(){
        if($this->hasRole('qudaojingli') || $this->hasRole('qudaozhuguan') || $this->hasRole('qudaozhuanyuan')){
            return $this->fetch('add-qudao');
        }elseif($this->hasRole('wangluojingli') || $this->hasRole('wangluozhuguan') || $this->hasRole('wangluozhuanyuan')){
            return $this->fetch('add-wangluo');
        }elseif ($this->hasRole(['yaoyuejingli','yaoyuezhuguan','yaoyuezhuanyuan'])){
            return $this->fetch('add-yaoyue');
        }elseif ($this->hasRole(['huodongjingli','huodongzhuguan','huodongzhuanyuan'])){
            return $this->fetch('add-huisuo');
        }else{
            return $this->fetch('add');
        }
    }

    public function getEditMoban(){
        if($this->hasRole('qudaojingli') || $this->hasRole('qudaozhuguan') || $this->hasRole('qudaozhuanyuan')){
            return $this->fetch('edit-qudao');
        }elseif($this->hasRole('wangluojingli') || $this->hasRole('wangluozhuguan') || $this->hasRole('wangluozhuanyuan')){
            return $this->fetch('edit-wangluo');
        }elseif($this->hasRole('yaoyuejingli') || $this->hasRole('yaoyuezhuguan') || $this->hasRole('yaoyuezhuanyuan')){
            return $this->fetch('edit-yaoyue');
        }elseif( $this->hasRole(['sheyingjingli','sheyingdiaodu']) ){
            return $this->fetch('edit-sheying');
        }elseif($this->hasRole('huodongjingli') || $this->hasRole('huodongzhuguan') || $this->hasRole('huodongzhuanyuan')){
            return $this->fetch('edit-huisuo');
        }else{
            return $this->fetch('edit');
        }
    }

}